package com.yazi.mxz.fieldsale.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myframework.smhj.common.tool.DateUtil;
import com.myframework.smhj.jackson.result.ObjectResult;
import com.myframework.smhj.jackson.result.StatusResult;
import com.yazi.mxz.fieldsale.entity.beans.FieldSale;
import com.yazi.mxz.fieldsale.entity.beans.FieldSaleGoods;
import com.yazi.mxz.fieldsale.repository.FieldSaleRepository;
import com.yazi.mxz.fieldsale.repository.StoreGoodsRepository;
import com.yazi.mxz.fieldsale.repository.StoreMemberRepository;

@Controller("fieldSaleControllerV1.1")
@RequestMapping("/v1.1")
public class FieldSaleController_V1_1 extends BaseController {

	@Resource
	FieldSaleRepository fieldSaleRepository;
	@Resource
	StoreGoodsRepository storeGoodsRepository;
	@Resource
	StoreMemberRepository storeMemberRepository;
	
	/**
	 * 销售主页面
	 * @param model
	 * @return
	 */
	@RequestMapping
	public String index(Model model) {
		model.addAttribute("version", getVersion());
		return "fieldSale_v1.1";
	}
	
	/**
	 * 获取(挂起的)销售单IDs
	 * @return
	 */
	@RequestMapping("/ids/json")
	@ResponseBody
	public ObjectResult<?> idsJson() {
		ObjectResult<List<FieldSale>> jsonResult = new ObjectResult<List<FieldSale>>();
		try {
			jsonResult.setResult(fieldSaleRepository.findByIsFinishedIsFalse());
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	/**
	 * 添加新销售单
	 * @return
	 */
	@RequestMapping("/addDo/json")
	@ResponseBody
	public ObjectResult<Long> addDoJson() {
		ObjectResult<Long> jsonResult = new ObjectResult<Long>();
		try {
			SimpleDateFormat sdf = new SimpleDateFormat("yyMMddhhmmss");
			
			FieldSale sale = new FieldSale();
			sale.setSerialCode(Long.parseLong(sdf.format(new Date())));
			sale.setIsFinished(false);
			sale.setAddTime(DateUtil.getTimeStamp());
			fieldSaleRepository.insert(sale);
			jsonResult.setResult(sale.getSerialCode());
			jsonResult.setOk();
			
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	/**
	 * 获取销售单详情
	 * @param serialCode
	 * @return
	 */
	@RequestMapping("/one/json")
	@ResponseBody
	public ObjectResult<?> oneJson(long serialCode) {
		ObjectResult<FieldSale> jsonResult = new ObjectResult<FieldSale>();
		FieldSale sale = null;
		try {
			sale = fieldSaleRepository.findOne(serialCode);
			List<FieldSaleGoods> saleGoodses = sale.getFieldSaleGoodses();
			if (null != saleGoodses) {
				for (FieldSaleGoods og : saleGoodses) {
					og.setStoreGoods(storeGoodsRepository.findOne(og.getStoreGoodsId()));
				}
			}
			jsonResult.setResult(sale);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		
		return jsonResult;
	}
	
	/**
	 * 重新保存
	 * @param serialCode
	 * @param listObj
	 * @return
	 */
	@RequestMapping("/updateDo/json")
	@ResponseBody
	public StatusResult updateDoJson(Long serialCode, ListObject listObj) {
		StatusResult jsonResult = new StatusResult();
		FieldSale sale;
		try {
			sale = fieldSaleRepository.findOne(serialCode);
			sale.setFieldSaleGoodses(listObj.getFsg());
			fieldSaleRepository.save(sale);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	/**
	 * 结算完成
	 * @param serialCode
	 * @param listObj
	 * @return
	 */
	@RequestMapping("/finishDo/json")
	@ResponseBody
	public StatusResult finishDoJson(Long serialCode, ListObject listObj) {
		StatusResult jsonResult = new StatusResult();
		FieldSale sale;
		try {
			sale = fieldSaleRepository.findOne(serialCode);
			sale.setCheckerId(storeMemberRepository.findAll().get(0).getMemberId());// TODO 指定一个营业员(临时方案)
			sale.setIsFinished(true);
			sale.setFieldSaleGoodses(listObj.getFsg());
			fieldSaleRepository.save(sale);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}

	/**
	 * 删除
	 * @param serialCode
	 * @return
	 */
	@RequestMapping("/delDo/json")
	@ResponseBody
	public StatusResult delDoJson(long serialCode) {
		StatusResult jsonResult = new StatusResult();
		try {
			fieldSaleRepository.delete(serialCode);
			jsonResult.setOk();
		} catch (Exception e) {
			jsonResult.setErrorMessage(e.getMessage());
		}
		return jsonResult;
	}
	
	/**
	 * 打印
	 * @return
	 */
	@RequestMapping("/print")
	public String print() {
		return "fieldSale_print_v1.1";
	}

	public static class ListObject {
		List<FieldSaleGoods> fsg;

		public List<FieldSaleGoods> getFsg() {
			return fsg;
		}

		public void setFsg(List<FieldSaleGoods> fsg) {
			this.fsg = fsg;
		}

	}
	
}
